/* @flow */
import React, { type Node } from 'react';
import { createAction, createActions, handleActions, type ActionType } from 'redux-actions';
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import produce from 'immer';
import { Icon } from 'antd';
import { SagaActionType, type sagaType } from 'lib/sagaUtil';
import Edittitleount from 'components/AccountingSubject/EditAccount';

/* ACTION TYPES */
// ModleDlg
const LOAD_ACCOUNTS = 'pay/titleounts/LOAD_ACCOUNTS';
const SET_ACCOUNTS = 'pay/titleounts/SET_ACCOUNTS';
const INSERT_ACCOUNTS = 'pay/titleounts/INSERT_ACCOUNTS';
const SHOW_MODELDLG = 'pay/titleounts/SHOW_MODELDLG';
const CLOSE_MODELDLG = 'pay/titleounts/CLOSE_MODELDLG';
const CHANGE_MODELDLG_TYPE = 'pay/titleounts/CHANGE_MODELDLG_TYPE';

/* ACTION PAYLOADS */

/* EXPORTING ACTION CREATORS / ACTION CREATORS TYPES */
export interface PayActionCreators {
  loadtitleounts(): any;
  inserttitleount(payload: any): any;
  showModledlg(): any;
  closeModledlg(): any;
  changeModledlgType(payload: string): any;
}
/* ACTION CREATORS */

export const actionCreators: PayActionCreators = {
  loadtitleounts: createAction(LOAD_ACCOUNTS),
  inserttitleount: createAction(INSERT_ACCOUNTS),
  showModledlg: createAction(SHOW_MODELDLG),
  closeModledlg: createAction(CLOSE_MODELDLG),
  changeModledlgType: createAction(CHANGE_MODELDLG_TYPE, (payload: string) => payload),
};
/* ACTION FLOW TYPE */
type LoadtitleountsAction = ActionType<typeof actionCreators.loadtitleounts>;
type ShowModledlgAction = ActionType<typeof actionCreators.showModledlg>;
type CloseModledlgAction = ActionType<typeof actionCreators.closeModledlg>;
type ChangeModledlgTypeAction = ActionType<typeof actionCreators.changeModledlgType>;

/* State Types */
type acount = {
  code: string,
  title: string,
  level: number,
  children: any,
};
export type Pay = {
  acounts: { modelDlg: { visible: boolean, type: string }, data: any },
};

const initialState: Pay = {
  acounts: {
    modelDlg: {
      visible: false,
      type: 'Edit',
      forms: {
        Edit: <Edittitleount />,
        Delete: <div>确定删除？</div>,
        Insert: <div>Insert</div>,
      },
    },
    data: [
      {
        code: '1',
        title: '资产类(Assets)',
        value: '1',
        key: '1',
        level: 0,
        children: [
          {
            code: '1001',
            title: '库存现金',
            value: '1-1',
            key: '1-1',
            level: 1,
            children: [
              {
                code: '1001001',
                title: 'cash',
                value: '1-1-1',
                key: '1-1-1',
                level: 2,
              },
            ],
          },
          {
            code: '1002',
            title: '银行存款',
            level: 1,
            children: [
              {
                code: '1002001',
                title: '借记卡',
                level: 2,
                children: [{ code: '1002001001', title: '中行U借记3420', level: 3 }],
              },
            ],
          },
          { code: '1003', title: '银行存款', level: 1 },
          { code: '1003', title: '存放中央银行款项', level: 1 },
          { code: '1011', title: '存放同业', level: 1 },
          { code: '1015', title: '其它货币基金', level: 1 },
          { code: '1021', title: '结算备付金', level: 1 },
          { code: '1031', title: '存出保证金', level: 1 },
          { code: '1051', title: '拆出资金', level: 1 },
          { code: '1101', title: '交易性金融资产', level: 1 },
          { code: '1111', title: '买入返售金融资产', level: 1 },
          { code: '1121', title: '应收票据', level: 1 },
          { code: '1122', title: '应收帐款', level: 1 },
          { code: '1123', title: '预付帐款', level: 1 },
          { code: '1131', title: '应收股利', level: 1 },
          { code: '1132', title: '应收利息', level: 1 },
          { code: '1211', title: '应收保护储金', level: 1 },
          { code: '1221', title: '应收代位追偿款', level: 1 },
          { code: '1222', title: '应收分保帐款', level: 1 },
          { code: '1223', title: '应收分保未到期责任准备金', level: 1 },
          { code: '1224', title: '应收分保保险责任准备金', level: 1 },
          { code: '1231', title: '其它应收款', level: 1 },
          { code: '1241', title: '坏帐准备', level: 1 },
          { code: '1251', title: '贴现资产', level: 1 },
          { code: '1301', title: '贷款', level: 1 },
          { code: '1302', title: '贷款损失准备', level: 1 },
          { code: '1311', title: '代理兑付证券', level: 1 },
          { code: '1321', title: '代理业务资产', level: 1 },
          { code: '1401', title: '材料采购', level: 1 },
          { code: '1402', title: '在途物资', level: 1 },
          { code: '1403', title: '原材料', level: 1 },
          { code: '1404', title: '材料成本差异', level: 1 },
          { code: '1406', title: '库存商品', level: 1 },
          { code: '1407', title: '发出商品', level: 1 },
          { code: '1410', title: '商品进销差价', level: 1 },
          { code: '1411', title: '委托加工物资', level: 1 },
          { code: '1412', title: '包装物及低值易耗品', level: 1 },
          { code: '1421', title: '消耗性物物资产', level: 1 },
          { code: '1431', title: '周转材料', level: 1 },
          { code: '1441', title: '贵金属', level: 1 },
          { code: '1442', title: '抵债资产', level: 1 },
          { code: '1451', title: '损余物资', level: 1 },
          { code: '1461', title: '存货跌价准备', level: 1 },
          { code: '1501', title: '待摊费用', level: 1 },
          { code: '1511', title: '独立帐户资产', level: 1 },
          { code: '1521', title: '持有至到期投资', level: 1 },
          { code: '1522', title: '持有至到期投资减值准备', level: 1 },
          { code: '1523', title: '可供出售金融资产', level: 1 },
          { code: '1524', title: '长期股权投资', level: 1 },
          { code: '1525', title: '长期股权投资减值准备', level: 1 },
          { code: '1526', title: '投资性房地产', level: 1 },
          { code: '1531', title: '长期应收款', level: 1 },
          { code: '1541', title: '未实现融资收益', level: 1 },
          { code: '1551', title: '存出资本保证金', level: 1 },
          { code: '1601', title: '固定资产', level: 1 },
          { code: '1602', title: '累计折旧', level: 1 },
          { code: '1603', title: '固定资产减值准备', level: 1 },
          { code: '1604', title: '在建工程', level: 1 },
          { code: '1605', title: '工程物资', level: 1 },
          { code: '1606', title: '固定资产清理', level: 1 },
          { code: '1611', title: '融资租赁资产', level: 1 },
          { code: '1612', title: '未担保余值', level: 1 },
          { code: '1621', title: '生产性生物资产', level: 1 },
          { code: '1622', title: '生产性生物资产累计折旧', level: 1 },
          { code: '1623', title: '公益性生物资产', level: 1 },
          { code: '1631', title: '油气资产', level: 1 },
          { code: '1632', title: '累计折耗', level: 1 },
          { code: '1701', title: '无形资产', level: 1 },
          { code: '1702', title: '累计摊销', level: 1 },
          { code: '1703', title: '无形资产减值准备', level: 1 },
          { code: '1711', title: '商誉', level: 1 },
          { code: '1801', title: '长期待摊费用', level: 1 },
          { code: '1811', title: '递延所得资产', level: 1 },
          { code: '1901', title: '待处理财产损益', level: 1 },
        ],
      },
      {
        code: '2',
        title: '负债类(Liabilities)',
        level: 0,
        children: [
          { code: '2001', title: '短期借款', level: 1 },
          { code: '2002', title: '存入保证金', level: 1 },
          { code: '2003', title: '拆入资金', level: 1 },
          { code: '2004', title: '向中央银行借款', level: 1 },
          { code: '2011', title: '同业存放', level: 1 },
          { code: '2012', title: '吸收存款', level: 1 },
          { code: '2021', title: '贴现负债', level: 1 },
          { code: '2101', title: '交易性金融负债', level: 1 },
          { code: '2111', title: '专出回购金融资产款', level: 1 },
          { code: '2201', title: '应付票据', level: 1 },
          { code: '2202', title: '应付帐款', level: 1 },
          { code: '2205', title: '预收帐款', level: 1 },
          { code: '2211', title: '应付职工薪酬', level: 1 },
          { code: '2221', title: '应交税费', level: 1 },
          { code: '2231', title: '应付股利', level: 1 },
          { code: '2232', title: '应付利息', level: 1 },
          {
            code: '2241',
            title: '其他应付款',
            level: 1,
            children: [
              {
                code: '2241001',
                title: '信用卡',
                level: 2,
                children: [
                  { code: '2241001001', title: '中行V信用5974', level: 3 },
                  { code: '2241001002', title: '中行U信用3775', level: 3 },
                ],
              },
            ],
          },
          { code: '2251', title: '应付保户红利', level: 1 },
          { code: '2261', title: '应付分保帐款', level: 1 },
          { code: '2311', title: '代理买卖证券款', level: 1 },
          { code: '2312', title: '代理承销证券款', level: 1 },
          { code: '2313', title: '代理兑付证券款', level: 1 },
          { code: '2314', title: '代理业务负债', level: 1 },
          { code: '2401', title: '预提费用', level: 1 },
          { code: '2411', title: '预计负债', level: 1 },
          { code: '2501', title: '递延收益', level: 1 },
          { code: '2601', title: '长期借款', level: 1 },
          { code: '2602', title: '长期债券', level: 1 },
          { code: '2701', title: '到期责任准备金', level: 1 },
          { code: '2702', title: '保险责任准备金', level: 1 },
          { code: '2711', title: '保户储金', level: 1 },
          { code: '2721', title: '独立帐户负债', level: 1 },
          { code: '2801', title: '长期应付款', level: 1 },
          { code: '2802', title: '未确认融资费用', level: 1 },
          { code: '2811', title: '专项应付款', level: 1 },
          { code: '2901', title: '递延所得税负债', level: 1 },
        ],
      },
      {
        code: '3',
        title: '共同类',
        level: 0,
        children: [
          { code: '3001', title: '清算资金往来', level: 1 },
          { code: '3002', title: '外汇买卖', level: 1 },
          { code: '3101', title: '衍生工具', level: 1 },
          { code: '3201', title: '套期工具', level: 1 },
          { code: '3202', title: '被套期项目', level: 1 },
        ],
      },
      {
        code: '4',
        title: '所有者权益类',
        level: 0,
        children: [
          { code: '4001', title: '实收资本', level: 1 },
          { code: '4002', title: '资本公积', level: 1 },
          { code: '4101', title: '盈余公积', level: 1 },
          { code: '4102', title: '一般风险准备', level: 1 },
          { code: '4103', title: '套期工具', level: 1 },
          { code: '4104', title: '利润分配', level: 1 },
          { code: '4201', title: '库存股', level: 1 },
        ],
      },
      {
        code: '5',
        title: '成本类',
        level: 0,
        children: [
          { code: '5001', title: '生产成本', level: 1 },
          { code: '5101', title: '制造费用', level: 1 },
          { code: '5201', title: '劳务成本', level: 1 },
          { code: '5301', title: '研发支出', level: 1 },
          { code: '5401', title: '工程施工', level: 1 },
          { code: '5402', title: '工程结算', level: 1 },
          { code: '5403', title: '机械作业', level: 1 },
        ],
      },
      {
        code: '6',
        title: '损益类',
        level: 0,
        children: [
          { code: '6001', title: '主营业务收入', level: 1 },
          { code: '6011', title: '利息收入', level: 1 },
          { code: '6021', title: '手续费收入', level: 1 },
          { code: '6031', title: '保费收入', level: 1 },
          { code: '6032', title: '分保费收入', level: 1 },
          { code: '6041', title: '租赁收入', level: 1 },
          { code: '6051', title: '其他业务收入', level: 1 },
          { code: '6061', title: '汇兑损益', level: 1 },
          { code: '6101', title: '公允价值变动损益', level: 1 },
          { code: '6111', title: '投资收益', level: 1 },
          { code: '6201', title: '摊回保险责任准备金', level: 1 },
          { code: '6202', title: '摊回赔付支出', level: 1 },
          { code: '6203', title: '摊回分保费用', level: 1 },
          { code: '6301', title: '营业外收入', level: 1 },
          { code: '6401', title: '主营业务成本', level: 1 },
          { code: '6402', title: '其它业务成本', level: 1 },
          { code: '6405', title: '营业税金及附加', level: 1 },
          { code: '6411', title: '利息支出', level: 1 },
          { code: '6421', title: '手续费支出', level: 1 },
          { code: '6501', title: '提取未到期责任准备金', level: 1 },
          { code: '6502', title: '撮保险责任准备金', level: 1 },
          { code: '6511', title: '赔付支出', level: 1 },
          { code: '6521', title: '保户红利支出', level: 1 },
          { code: '6531', title: '退保金', level: 1 },
          { code: '6541', title: '分出保费', level: 1 },
          { code: '6542', title: '分保费用', level: 1 },
          { code: '6601', title: '销售费用', level: 1 },
          {
            code: '6602',
            title: '管理费用',
            level: 1,
            children: [{ code: '6602001', title: '个人旅游', level: 2 }],
          },
          { code: '6603', title: '财务费用', level: 1 },
          { code: '6604', title: '勘探费用', level: 1 },
          { code: '6701', title: '资产减值损失', level: 1 },
          { code: '6711', title: '营业外支出', level: 1 },
          { code: '6801', title: '所得税', level: 1 },
          { code: '6901', title: '以前年度损益调整', level: 1 },
        ],
      },
    ],
  },
};

export default handleActions(
  {
    [SHOW_MODELDLG]: (state) => {
      return produce(state, (draft) => {
        draft.acounts.modelDlg.visible = true;
      });
    },
    [CLOSE_MODELDLG]: (state) => {
      return produce(state, (draft) => {
        draft.acounts.modelDlg.visible = false;
      });
    },
    [CHANGE_MODELDLG_TYPE]: (state, action: ChangeModledlgTypeAction) => {
      return produce(state, (draft) => {
        draft.acounts.modelDlg.type = action.payload;
      });
    },
    [SagaActionType(INSERT_ACCOUNTS).SUCCEEDED]: (state, action: ChangeModledlgTypeAction) => {
      return produce(state, (draft) => {
        draft.acounts.modelDlg.type = action.payload;
      });
    },
  },
  initialState,
);
function* inserttitleountSaga() {
  try {
    // const user = yield call(Api.fetchUser, action.payload.userId);
    yield put({ type: '' });
    yield put({ type: SagaActionType(INSERT_ACCOUNTS).SUCCEEDED, payload: '' });
  } catch (e) {
    yield put({ type: SagaActionType(INSERT_ACCOUNTS).FAILED, message: e.message });
  }
}
export function* RootSaga(): * {
  yield takeLatest(INSERT_ACCOUNTS, inserttitleountSaga);
}
